package com.ys.controller.web.logistics;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.logistics.TpAmazonAddress;
import com.ys.service.flow.YspTaskService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报关单
 */
@Controller
@RequestMapping("/web/logistics/tpAmazonAddress")
public class WebTpAmazonAddressController {
	private static final Logger logger = LoggerFactory.getLogger(WebTpAmazonAddressController.class);

	@Autowired
	private CommonApiService<TpAmazonAddress> commonApiService;

    @Autowired
    private YspTaskService yspTaskService;


    private final Long MENU_ID = MenuId.TP_AmazonAddress;

    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> extraParam = new HashMap<>();
            //关联数据
            extraParam.put("status", 1);
//
            TpAmazonAddress entity = new TpAmazonAddress();
//            //关联数据
//            Long id = commonApiService.getCommonNextBid(MENU_ID);
//            entity.setId(id);
//
//            // 子列表配置信息
//            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID,new ArrayList<>());
            return commonApiService.toAdd(model, entity, MENU_ID, extraParam, null);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            Map<String, Object> extraParam = getExtraParam(id);

//            List<Map<String, Object>> itemList = getItemListByBid(id);
            // 子列表配置信息
//            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID,itemList);
//
//            //子表按钮
//            Integer state = yspTaskService.flowState(bid);
//            if (state == Flow.State.FINISH.value){
//                subGridConfig.setButtonArr(new ArrayList<>());
//            }else if (state == Flow.State.PROCESS.value){
//                subGridConfig.setButtonArr(new ArrayList<>());
//            }
            return commonApiService.toEdit(model,Long.parseLong(extraParam.get("id").toString()), new TpAmazonAddress(), MENU_ID, extraParam, null);

        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    public Map<String,Object> getExtraParam(Long id){
        Map<String,Object> extraParam = new TpAmazonAddress()
                .addLeftJoin("TB_Country B ON B.Code = A.CountryCode")
                .addField("B.Name countryName,B.Code countryCode")
                .setId(id)
                .queryFirstMap();

        return extraParam;
    }




}








